package com.sample.xcrash;

import com.sample.xcrash_lib.*;
import ohos.aafwk.ability.AbilityPackage;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

import static org.junit.Assert.*;

public class TombstoneManagerTest extends AbilityPackage {

    private static final String javaLogSuffix = ".java.xcrash";
    private static final String nativeLogSuffix = ".native.xcrash";
    private static final String anrLogSuffix = ".anr.xcrash";

    Init init = new Init();;

    @Before
    public void setUp() throws Exception {

        init.setUp();

    }

    @After
    public void tearDown() throws Exception {
    }

    @Test
    public void isJavaCrashTest() {
        File file = new File(javaLogSuffix);
        boolean state = TombstoneManager.isJavaCrash(file);
        assertTrue(state);
    }

    @Test
    public void isNativeCrashTest() {
        File file = new File(nativeLogSuffix);
        boolean state = TombstoneManager.isNativeCrash(file);
        assertTrue(state);
    }

    @Test
    public void isAnrTest() {
        File file = new File(anrLogSuffix);
        boolean state = TombstoneManager.isAnr(file);
        assertTrue(state);
    }

    @Test
    public void getJavaTombstonesTest() {
        assertArrayEquals(getFiles(new String[]{javaLogSuffix}), TombstoneManager.getJavaTombstones());
    }

    @Test
    public void getNativeTombstonesTest() {
        assertArrayEquals(getFiles(new String[]{nativeLogSuffix}), TombstoneManager.getJavaTombstones());
    }

    @Test
    public void getAnrTombstonesTest() {
        assertArrayEquals(getFiles(new String[]{anrLogSuffix}), TombstoneManager.getJavaTombstones());
    }

    @Test
    public void getAllTombstonesTest() {
        assertArrayEquals(getFiles(new String[]{javaLogSuffix, nativeLogSuffix, anrLogSuffix}), TombstoneManager.getJavaTombstones());
    }

    @Test
    public void clearJavaTombstonesTest() {
        boolean state = TombstoneManager.clearJavaTombstones();
        assertTrue(state);
    }

    @Test
    public void clearNativeTombstonesTest() {
        boolean state = TombstoneManager.clearNativeTombstones();
        assertTrue(state);
    }

    @Test
    public void clearAnrTombstonesTest() {
        boolean state = TombstoneManager.clearAnrTombstones();
        assertTrue(state);
    }

    @Test
    public void clearAllTombstonesTest() {
        boolean state = TombstoneManager.clearAllTombstones();
        assertTrue(state);
    }

    public File[] getFiles(String[] str) {
        String logDir = XCrash.getLogDir();
        File dir = new File(logDir);

        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File file, String s) {
                if (!s.startsWith("tombstone" + "_")) {
                    return false;
                }
                for (String logPrefix : str) {
                    if (s.endsWith(logPrefix)) {
                        return true;
                    }
                }
                return false;
            }
        });

        //sort
        Arrays.sort(files, new Comparator<File>() {
            @Override
            public int compare(File f1, File f2) {
                return f1.getName().compareTo(f2.getName());
            }
        });

        return  files;
    }

}
